// 拼接HTTPUrl
function url(type, pathname) {
  return window.RPConfig[type] + pathname;
}

const sysConfigApi = {
  getCurrentConfig: params => {
    return $http.get(url('itShare', `/systemConfig/currentConfig?reportName=${params.reportName}`), {});
  },
  autoRefreshRecord: params => {
    return $http.post(url('itShare', `/systemConfig/autoRefresh/operateRecord`), params);
  },
  statisticConfigRecord: params => {
    return $http.post(url('itShare', `/systemConfig/statisticConfig/operateRecord`), params);
  },
  permissionRecord: params => {
    return $http.post(url('itShare', `/systemConfig/permission/operateRecord`), params);
  },
  autoRefreshSubmit: params => {
    return $http.post(url('itShare', `/systemConfig/autoRefresh/submit`), params);
  },
  queryAutoRefreshSubmitStatus: params => {
    return $http.post(url('itShare', `/systemConfig/autoRefresh/submit/result?id=${params.id}`), {});
  },
  permissionSubmit: params => {
    return $http.post(url('itShare', `/systemConfig/permission/submit`), params);
  },
  queryPermissionSubmitStatus: params => {
    return $http.post(url('itShare', `/systemConfig/permission/submit/result?id=${params.id}`), {});
  },
  countSubmit: params => {
    return $http.post(url('itShare', `/rptPlantaskStypeData/calculatePlanTaskData`), params);
  },
  // 获取统计周期list
  getCycleList: params => {
    return $http.get(url('itShare', `/rptPlantaskDataStat/getStatCycleList`), params);
  },
  // 获取登录人业务单元list
  getBusinessUnitList: params => {
    return $http.get(url('itShare', `/rptPlantaskDataStat/getTrgNodeList`), params);
  },
  // 查看任务表统计信息
  getTaskStatisticData: params => {
    return $http.post(url('itShare', `/rptPlantaskDataStat/queryTaskStatisticData`), params);
  },
  // 查看任务表统计信息
  getMeasureAndAreaStaticData: params => {
    return $http.post(url('itShare', `/rptPlantaskDataStat/queryMeasureAndAreaStaticData`), params);
  },
  // 下载统计数据
  // statCycle：填报周期（示例：2023-1）
  // trgNode：业务单元（示例：全集团）
  // reportNum：报表编码（任务状态占比报表：tsp，提交情况：ss，不同举措完成情况：dm，不同领域完成情况：da）
  downLoadData: (params) => {
    let { statCycle, trgNode, reportNum } = params
    const downloadUrl = url('itShare', `/rptPlantaskDataStat/export`) + `?statCycle=${statCycle}&trgNode=${trgNode}&reportNum=${reportNum}`
    const xhr = new XMLHttpRequest()
    xhr.open('GET', downloadUrl, true)
    xhr.setRequestHeader('authorization', localStorage.Authorization ? `Bearer ${localStorage.Authorization}` : '')
    xhr.responseType = 'blob'
    xhr.onload = (e) => {
      const _this = e.target
      if (_this.status === 200) {
        const blob = _this.response
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = ''
        let headersName = decodeURI(xhr.getResponseHeader("content-disposition").split("=")[1])
        headersName = decodeURIComponent(escape(headersName))
        a.setAttribute('download', headersName)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      }
    }
    xhr.send({})
  },
};

export default sysConfigApi;
